import 'package:dear_lottery/tools/DL_Config.dart';
import 'package:dear_lottery/tools/DL_drawNumbersView.dart';
import 'package:flutter/material.dart';
import 'package:hexcolor/hexcolor.dart';
import 'package:intl/intl.dart';

import '../tools/DL_Segment.dart';
import '../tools/models/DL_DataViewModel.dart';
import '../tools/models/DL_LottoModel.dart';
import 'DL_resultDetailPageVC.dart';

class DL_ResultsPageVC extends StatefulWidget {
  const DL_ResultsPageVC({super.key});

  @override
  State<DL_ResultsPageVC> createState() => _DL_ResultsPageVCState();
}

class _DL_ResultsPageVCState extends State<DL_ResultsPageVC>
    with AutomaticKeepAliveClientMixin {
  ///列表数据
  late List _results = [];

  late var onePMModels = [];
  late var sixPMModels = [];
  late var eightPMModels = [];
  late var otherPMModels = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    _configData();
    _onTabChanged(0);
  }

  void _configData() {
    onePMModels = DL_DataViewModel().DL_onePM_resultsModels;
    sixPMModels = DL_DataViewModel().DL_sixPM_resultsModels;
    eightPMModels = DL_DataViewModel().DL_eightPM_resultsModels;
    otherPMModels = DL_DataViewModel().DL_others_resultsModels;
  }

  void _onTabChanged(int index) {
    switch (index) {
      case 0:
        _results = onePMModels;
        break;
      case 1:
        _results = sixPMModels;
        break;
      case 2:
        _results = eightPMModels;
        break;
      case 3:
        _results = otherPMModels;
        break;
      default:
    }
    setState(() {});
  }

  ///
  void _goToDetailPage(DL_LottoModel model) {
    Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) => DL_ResultDetailPageVC(
                  lottoModel: model,
                )));
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      appBar: AppBar(
        title: Image.asset(
          'assets/sys_imgs/DearLottery Logo.png',
          width: 160,
          height: 50,
        ),
        backgroundColor: DL_Config.DL_MainColor,
        foregroundColor: DL_Config.DL_navgatBarTitleColor,
        centerTitle: true,
      ),
      body: Container(
        color: DL_Config.DL_pageBgColor,
        child: Column(children: [
          Container(
            width: double.infinity,
            height: 40,
            color: Colors.white,
            child: SizedBox(
              height: 40,
              child: Padding(
                padding: const EdgeInsets.only(left: 10, right: 10),
                child: DlSegment(
                  sliderColor: HexColor('#ff3433'),
                  onSegmentChange: (index, value) => {
                    debugPrint('value---$value,index---$index'),
                    _onTabChanged(index),
                  },
                ),
              ),
            ),
          ),
          _results.isEmpty
              ? const Expanded(
                  child: Center(
                      child: Text(
                    'No Data',
                  )),
                )
              : Expanded(
                  child: ListView.builder(
                      itemCount: _results.length,
                      itemBuilder: (context, index) {
                        DL_LottoModel model = _results[index];
                        // return resultListCell(model);
                        return resultListCell(model);
                      }),
                ),
        ]),
      ),
    );
  }

  Widget resultListCell(DL_LottoModel model) {
    var name = model.lottery_name!;
    DateTime drawDateTimeUTC = DateTime.parse(model.draw_date!);
    var dateTimeString = DateFormat('d/MMM/y').format(drawDateTimeUTC);

    var numbersMap = {};
    List balls = [];
    if (model.first_prize_num!.isNotEmpty) {
      String numberStr = model.first_prize_num![0];
      // // 使用 split 方法将字符串转换为字符串数组
      List<String> charArray = numberStr.split('');
      for (var i = 0; i < charArray.length; i++) {
        var ball = {
          'number': charArray[i],
          'isRight': false,
          'isNormal': true
        }; //isNormal代表是否对比中奖号码，true不用对比，false需要对比。isRight代表是否中奖号码
        balls.add(ball);
      }
      var title = '1st Prize';
      numbersMap = {'title': title, 'balls': balls};
    }

    return GestureDetector(
        onTap: () {
          debugPrint('resultListCell');
          _goToDetailPage(model);
        },
        child: Container(
          margin: const EdgeInsets.fromLTRB(15, 15, 15, 1),
          height: 140,
          decoration: const BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.all(Radius.circular(10)),
          ),
          child: Column(children: [
            Container(
              margin: const EdgeInsets.only(left: 10, right: 10),
              height: 40,
              // color: Colors.yellow,
              child: Row(children: [
                Text(
                  name,
                  style: TextStyle(
                      fontWeight: FontWeight.w700,
                      fontSize: 14,
                      color: HexColor('#D32F7D')),
                ),
                const Spacer(),
                Text(
                  dateTimeString,
                  style: TextStyle(
                      fontWeight: FontWeight.w700,
                      fontSize: 15,
                      color: HexColor('#999999')),
                ),
              ]),
            ),
            Expanded(
                child: SizedBox(
              width: double.infinity,
              child: Row(
                children: [
                  Expanded(
                      child: Container(
                    // color: Colors.red,
                    alignment: Alignment.centerLeft,
                    // child: DL_drawNumbersView(data: numbersMap),
                    child: drawingNumbersView(numbersMap),
                  )),
                  assetTagView(),
                ],
              ),
            )),
          ]),
        ));
  }

  Widget drawingNumbersView(var numbersMap) {
    return Container(
      margin: const EdgeInsets.all(10),
      child: DL_drawNumbersView(data: numbersMap),
    );
  }

  Widget assetTagView() {
    return Container(
      alignment: Alignment.center,
      height: double.infinity,
      width: 30,
      decoration: const BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.only(
            topRight: Radius.circular(10),
            bottomRight: Radius.circular(10),
          )),
      child: Image.asset(
        'assets/sys_imgs/EJ_union.png',
        width: 8.0,
        height: 13.0,
      ),
    );
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
